﻿
using QFramework;
using System.Collections.Generic;
using UnityEngine;

namespace ProjectChildhood
{
    /// <summary>
    /// 土地系统功能模块
    /// </summary>
    public interface IGridSystem : QFramework.IToolBarSystem
    {
        EasyGrid<SoilData> GridDatas { get; }

        EasyGrid<IPlant> PlantDatas { get; }

        void ResetData();
        void SaveData();
        void LoadData();
    }

    public class GridSystem : AbstractSystem, IGridSystem
    {
        public EasyGrid<SoilData> GridDatas { get; } = new EasyGrid<SoilData>(Config.INIT_GRID_WIDTH, Config.INIT_GRID_HEIGHT);

        public EasyGrid<IPlant> PlantDatas { get; } = new EasyGrid<IPlant>(Config.INIT_GRID_WIDTH, Config.INIT_GRID_HEIGHT);

        public void LoadData()
        {
            int width = PlayerPrefs.GetInt("grid_width", Config.INIT_GRID_WIDTH);
            int height = PlayerPrefs.GetInt("grid_height", Config.INIT_GRID_HEIGHT);
            GridDatas.Resize(width, height, (x, y) => null);

            //加载地块数据
            for (int i = 0; i < GridDatas.Width; i++)
            {
                for (int j = 0; j < GridDatas.Height; j++)
                {
                    bool isEmpty = PlayerPrefs.GetInt($"soil_{i}_{j}_is_empty", 1) == 1 ? true : false;
                    if (!isEmpty)
                    {
                        bool hasPlant = PlayerPrefs.GetInt($"soil_{i}_{j}_has_plant", 0) == 1 ? true : false;
                        GridDatas[i, j] = new SoilData();
                        GridDatas[i, j].HasPlant = hasPlant;

                        if (hasPlant)
                        {
                            string plantName = PlayerPrefs.GetString($"soil_{i}_{j}_plant_name", string.Empty);
                            EPlantState plantState = (EPlantState) PlayerPrefs.GetInt($"soil_{i}_{j}_plant_state", (int)EPlantState.Seed);
                            int daysInCurrentState = PlayerPrefs.GetInt($"soil_{i}_{j}_days_in_current_state", 0);
                            GridDatas[i, j].PlantName = plantName;
                            GridDatas[i, j].PlantState = plantState;
                            GridDatas[i, j].DaysInCurrentState = daysInCurrentState;
                        }
                    }
                    else 
                    {
                        GridDatas[i, j] = null;
                    }
                    
                }
            }
        }

        public void ResetData()
        {
            for (int i = 0; i < GridDatas.Width; i++)
            {
                for (int j = 0; j < GridDatas.Height; j++)
                {
                    PlayerPrefs.SetInt($"soil_{i}_{j}_is_empty", 1); // 土地置空
                    PlayerPrefs.SetInt($"soil_{i}_{j}_has_plant", false ? 1 : 0);
                    PlayerPrefs.SetString($"soil_{i}_{j}_plant_name", string.Empty);
                    PlayerPrefs.SetInt($"soil_{i}_{j}_plant_state", 0);
                    PlayerPrefs.SetInt($"soil_{i}_{j}_days_in_current_state", 0);
                    GridDatas[i,j] = null;
                }
            }

            GridDatas.Resize(Config.INIT_GRID_WIDTH, Config.INIT_GRID_HEIGHT, (x,y)=> null);
            
            PlayerPrefs.SetInt("grid_width", GridDatas.Width);
            PlayerPrefs.SetInt("grid_height", GridDatas.Height);
        }

        public void SaveData()
        {
            PlayerPrefs.SetInt("grid_width", GridDatas.Width);
            PlayerPrefs.SetInt("grid_height", GridDatas.Height) ;
            for (int i = 0; i < GridDatas.Width; i++)
            {
                for (int j = 0; j < GridDatas.Height; j++)
                {
                    SoilData soilData = GridDatas[i, j];
                    if (soilData == null)
                    {
                        PlayerPrefs.SetInt($"soil_{i}_{j}_is_empty", true ? 1 : 0);
                        PlayerPrefs.SetInt($"soil_{i}_{j}_has_plant", false ? 1 : 0);
                    }
                    else
                    {
                        PlayerPrefs.SetInt($"soil_{i}_{j}_is_empty", false ? 1 : 0);
                        PlayerPrefs.SetInt($"soil_{i}_{j}_has_plant", soilData.HasPlant ? 1 : 0);
                        if (soilData.HasPlant)
                        {
                            PlayerPrefs.SetString($"soil_{i}_{j}_plant_name", soilData.PlantName);
                            PlayerPrefs.SetInt($"soil_{i}_{j}_plant_state", (int)soilData.PlantState);
                            PlayerPrefs.SetInt($"soil_{i}_{j}_days_in_current_state", soilData.DaysInCurrentState);
                        }
                    }
                }
            }
        }

        protected override void OnInit()
        {

        }
    }
}
